package com.yyjj.hangqing.kline;


import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.vinsonguo.klinelib.chart.KLineView;
import com.vinsonguo.klinelib.model.HisData;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.GetRequest;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.entity.BaseTResp;
import com.yyjj.hangqing.entity.KLineCoinEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.DateUtil;


public class KLineChartCoinFragment extends BaseFragment{

    public static final String TYPE_5_MINUTE = "2";
    public static final String TYPE_15_MINUTE = "3";
    public static final String TYPE_30_MINUTE = "4";
    public static final String TYPE_60_MINUTE = "5";
    public static final String TYPE_DAY = "6";

    private static final String KEY_CODE = "code";
    private static final String KEY_IS_DOMESTIC = "isDomestic";
    private static final String KEY_TYPE = "type";

    private String path;
    private KLineView mKLineView;
    private View layoutEmpty;
    private View layoutError;
    private View layoutLoading;
    private TextView mTvRetry;

    private String code;
    private boolean isDomestic;
    private String type;
    private String timeType;

    public static KLineChartCoinFragment newInstance(String code, String timeType, String type) {
        KLineChartCoinFragment fragment = new KLineChartCoinFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_CODE, code);
        bundle.putString(KEY_TYPE, type);
        bundle.putString("timeType", timeType);
        fragment.setArguments(bundle);
        return fragment;
    }
    public static KLineChartCoinFragment newInstance(String code, String timeType, String type, String path) {
        KLineChartCoinFragment fragment = new KLineChartCoinFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_CODE, code);
        bundle.putString(KEY_TYPE, type);
        bundle.putString("timeType", timeType);
        bundle.putString("path", path);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void beforeSetContentView() {
        super.beforeSetContentView();
        code = getArguments().getString(KEY_CODE);
        type = getArguments().getString(KEY_TYPE);
        timeType = getArguments().getString("timeType");
        path = getArguments().getString("path");
    }

    public void showVolume() {

        mKLineView.post(new Runnable() {
            @Override
            public void run() {
                mKLineView.showVolume();
            }
        });
    }

    public void showMacd() {
        mKLineView.post(new Runnable() {
            @Override
            public void run() {
                mKLineView.showMacd();
            }
        });
    }

    public void showKdj() {
        mKLineView.post(new Runnable() {
            @Override
            public void run() {
                mKLineView.showKdj();
            }
        });
    }

    @Override
    public int getContentLayout() {
        return R.layout.hq_fragment_kline_chart;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        layoutEmpty = mContentView.findViewById(R.id.layout_empty);
        layoutError = mContentView.findViewById(R.id.layout_error);
        layoutLoading = mContentView.findViewById(R.id.layout_loading);
        mTvRetry = layoutError.findViewById(R.id.tv_retry);
        mKLineView = mContentView.findViewById(R.id.kline);
        switch (type) {
            case TYPE_5_MINUTE:
            case TYPE_15_MINUTE:
            case TYPE_30_MINUTE:
            case TYPE_60_MINUTE:
                mKLineView.setDateFormat(DateUtil.FORMAT_102);
                break;
            case TYPE_DAY:
                mKLineView.setDateFormat(DateUtil.FORMAT_3);
                break;
        }
        showVolume();


        mTvRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });

    }



    @Override
    public void loadData() {
        showTheLoading();
        GetRequest request;
        if(path ==null){
            request = ViseHttp.GET("/yapi/Line/index")
                    .addParam("symbol", code)
                    .addParam("type", type)
                    .addParam("line_type", timeType);
        }else{
//            http://data.fk7h.com/yapi/wankenet/get_line?name=wkb_bitcny&time=5
            request = ViseHttp.GET(path)
                    .addParam("name", code)
                    .addParam("time", type);
        }
//                .addParam("is_domestic", 2+"")
//                .addParam("ti", 2+"")
        request.request(new ACallback<BaseTResp<KLineCoinEntity>>() {
            @Override
            public void onSuccess(BaseTResp<KLineCoinEntity> data) {
                if (1 == data.code) {
                    KLineCoinEntity kLine = data.data;
                    if (kLine != null ) {
                        List<HisData> hisData = new ArrayList<>();
                        String[] C = kLine.getC().split(",");
                        String[] O = kLine.getO().split(",");
                        String[] H = kLine.getH().split(",");
                        String[] L = kLine.getL().split(",");
                        String[] V = kLine.getV().split(",");
                        String[] T = kLine.getT().split(",");

                        int size = Collections.min(Arrays.asList(C.length,O.length,H.length,L.length,V.length,T.length));

                        for (int i = 0; i < size; i++) {
                            HisData item = new HisData();
                            item.setClose(DataUtils.stringToDouble(C[i]));
                            item.setOpen(DataUtils.stringToDouble(O[i]));
                            item.setHigh(DataUtils.stringToDouble(H[i]));
                            item.setLow(DataUtils.stringToDouble(L[i]));
                            item.setVol(DataUtils.stringToLong(V[i]));
                            item.setDate(DateUtil.parseTime(T[i]).getTime());
                            hisData.add(item);
                        }

                        mKLineView.initData(hisData);
                        mKLineView.setLimitLine();
                        showContent();
                    }
                } else {
                    showEmpty();
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                showError();
            }
        });
    }

    public void showTheLoading() {
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.GONE);
        layoutLoading.setVisibility(View.VISIBLE);
    }

    private void showEmpty() {
        layoutEmpty.setVisibility(View.VISIBLE);
        layoutError.setVisibility(View.GONE);
        layoutLoading.setVisibility(View.GONE);
    }

    private void showError() {
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.VISIBLE);
        layoutLoading.setVisibility(View.GONE);
    }

    private void showContent() {
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.GONE);
        layoutLoading.setVisibility(View.GONE);
    }


//    @Override
//    public boolean isRegisterEvent() {
//        return true;
//    }
//
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onRefreshKLineEvent(RefreshKLineEvent event) {
//        loadData();
//    }

}
